

let data = {
  name:'zhaobo',
  age:18,
  arr:[1,2,3],
};
let ejs = require('ejs');
let fs = require('fs')
let path = require('path')

let tempStr = fs.readFileSync(path.resolve(__dirname,'index.html'),'utf8');

// function render(str,data){
//   return str.replace(/<%=([\s\S]*?)%>/g,function () {
//     return data[arguments[1]];
//   });
// }
function render(str,data){
  str = str.replace(/<%=([\s\S]*?)%>/g,function () {
    return '${'+arguments[1]+'}';
  });

  let head = 'let str;\r\nwith(data){\r\n'
   head += 'str=`';
  let content = str.replace(/<%([\s\S]*?)%>/g,function(){
    return '`\r\n'+arguments[1]+'\r\nstr+=`'

  });
  let tail = '`\r\n}\r\nreturn str;';
  let fn =  new Function('data',head+content+tail);
  return fn(data);

}
let str = render(tempStr,data);
// let str = ejs.render(tempStr,data);
console.log(str);
